#include "lwip/netifapi.h"
#include "string.h"
#include "uart.h"
#include "lwip/sockets.h"
#include "lwip/ip4_addr.h"
#include "lwip/netdb.h"
#include "soc_osal.h"

#define CONFIG_UART_BUS_ID      1
#define HTTPC_DEMO_RECV_BUFSIZE 2048  
#define SOCK_TARGET_PORT  80  
#define SERVER_HOST   "t.weather.itboy.net"
static const char *g_request = "GET /api/weather/city/101190101 HTTP/1.1\r\nHost: t.weather.itboy.net\r\nConnection: close\r\n\r\n"; 
char response[HTTPC_DEMO_RECV_BUFSIZE];

void data_send(uint8_t *data,uint32_t length)
{
    uapi_uart_write(CONFIG_UART_BUS_ID,data,length,0);
}

void get_type(char *response)
{
    char *ptr = strstr(response,"type");
    ptr[13] = '\0';
    ptr = ptr + 7;
    osal_printk("\r\ncmp string: %s\r\n",ptr);
    
    if(ptr[0] == 0xE5 && ptr[1] == 0xA4 && ptr[2] == 0x9A && ptr[3] == 0xE4 && ptr[4] == 0xBA && ptr[5] == 0x91)
    {
        osal_printk("cloud day\r\n");
        const char *ptr = "B多云";
        data_send((uint8_t *)ptr,8);
    }
    else if(ptr[0] == 0xE5 && ptr[1] == 0xB0 && ptr[2] == 0x8F && ptr[3] == 0xE9 && ptr[4] == 0x9B && ptr[5] == 0xA8)
    {
        osal_printk("small rainy day\r\n");
        const char *ptr = "B小雨";
        data_send((uint8_t *)ptr,8);
    }
    else if(ptr[0] == 0xE4 && ptr[1] == 0xB8 && ptr[2] == 0xAD && ptr[3] == 0xE9 && ptr[4] == 0x9B && ptr[5] == 0xA8)
    {
        osal_printk("medium rainy day\r\n");
        const char *ptr = "B中雨";
        data_send((uint8_t *)ptr,8);
    }
    else if(ptr[0] == 0xE5 && ptr[1] == 0xA4 && ptr[2] == 0xA7 && ptr[3] == 0xE9 && ptr[4] == 0x9B && ptr[5] == 0xA8)
    {
        osal_printk("big rainy day\r\n");
        const char *ptr = "B大雨";
        data_send((uint8_t *)ptr,8);
    }
    else if(ptr[0] == 0xE9 && ptr[1] == 0x98)
    {
        osal_printk("overcast day\r\n");
        const char *ptr = "B阴";
        data_send((uint8_t *)ptr,5);
    }
    else if(ptr[0] == 0xE6 && ptr[1] == 0x99)
    {
        osal_printk("overcast day\r\n");
        const char *ptr = "B晴";
        data_send((uint8_t *)ptr,5);
    }
}

void get_wendu(char *response)
{
    char buff[20];
    char head = 'A';
    char *ptr = strstr(response,"wendu");
    ptr[12] = '\0';
    ptr = ptr + 8;
    sprintf_s(buff,sizeof(buff),"%c%s",head,ptr);
    osal_printk("\r\nget string: %s\r\n",buff);
    osal_printk("\r\ncmp string: %s\r\n",ptr);
    data_send((uint8_t *)buff,8);
}

void get_shidu(char *response)
{
    char buff[20];
    char head = 'G';
    char *ptr = strstr(response,"shidu");
    ptr[11] = '\0';
    ptr = ptr + 8;
    sprintf_s(buff,sizeof(buff),"%c%s",head,ptr);
    osal_printk("\r\nget string: %s\r\n",buff);
    osal_printk("\r\ncmp string: %s\r\n",ptr);
    data_send((uint8_t *)buff,8);
}
/*
void get_week(char *response)
{
    char buff[20];
    char head = 'H';
    char *ptr = strstr(response,"week");
    if(ptr == NULL)
    {
        osal_printk("ptr is null\r\n");
        return;
    }
    ptr[17] = '\0';
    ptr = ptr + 7;
    sprintf_s(buff,sizeof(buff),"%c%s",head,ptr);
    osal_printk("\r\nget string: %s\r\n",buff);
    osal_printk("\r\ncmp string: %s\r\n",ptr);
    data_send((uint8_t *)buff,8);
}*/

int http_clienti_get(void)
{
    struct sockaddr_in addr = {0};
    struct hostent *server;
    int sockfd;
    sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(sockfd < 0)
    {
        osal_printk("socket init fail!!\r\n");
        return -1;
    }
    server = gethostbyname(SERVER_HOST);
    if(server == NULL)
    {
        osal_printk("Fail to get ip\r\n");
        return -1;
    }
    // osal_printk("IP name : %s\r\n",server->h_name);
    // osal_printk("IP : %s\r\n",server->h_addr_list[0]);
    addr.sin_family = AF_INET;
    addr.sin_port = htons(SOCK_TARGET_PORT);
    //addr.sin_addr.s_addr = inet_addr(SERVER_IP);
    memcpy(&addr.sin_addr.s_addr, server->h_addr_list[0], server->h_length);
    if(connect(sockfd,(struct sockaddr*)&addr,sizeof(addr)) != 0)
    {
        osal_printk("sock connect fail\r\n");
        lwip_close(sockfd);
        return -1;
    }
    osal_printk("sock connect succ\r\n");
    
    if(send(sockfd,g_request,strlen(g_request),0) < 0)
    {
        osal_printk("sock send fail\r\n");
        lwip_close(sockfd);
        return -1;
    }
    osal_mdelay(3000);
    memset(response,0,sizeof(response));
    if(recv(sockfd,response,sizeof(response),0) < 0)
    {
        osal_printk("sock recv fail\r\n");
        lwip_close(sockfd);
        return -1;
    }
    //osal_printk("recv:\r\n%s\r\n",response);
    get_type(response);
    osal_msleep(100);
    //get_week(response);
    //osal_msleep(100);
    get_wendu(response);
    osal_msleep(100);
    get_shidu(response);
    lwip_close(sockfd);
    memset(response,0,sizeof(response));
    return 0;
}
